<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%@ include file="/common/user.jsp"%>
<%@ include file = "/common/operatehistory.jsp"%>
<%@ include file="/common/visit.jsp"%>
<%
	hospital = 999;
    String now = DateUtil.formatDate(new Date());
	List<Map> visitList = QBuilder.db("basedb").select("new map(v.id as point_id, v.surveyId as survey_id, v.patientUid as patient_uid, sq.title as title, sq.showOrder as show_order, sq.detail as detail, sq.surveyId as parent_survey_id)").from("Visit as v, SurveyQuestion as sq").where(new Where("v.hospital=", hospital).and("v.category=", SURVEY_QUESTION_CATEGORY).and("v.status=", STATUS_ENABLE).and("sq.id=v.surveyId").and("v.isconfirmed=", NO_PUSH).and("v.isconfirmed=", NO_PUSH).and("send_time like '%" + now + "%'")).query().getResultObj();
	Map<Integer, Integer> patientSuveyMap = new HashMap();
	String newsIds = "-1";
	String questIds = "-1";
	String parentSurveyIds = "-1";
	List<Integer> uids = new ArrayList<Integer>();
	for (Map map:visitList) {
		parentSurveyIds = parentSurveyIds + "," + map.get("parent_survey_id");
		uids.add((Integer)map.get("patient_uid"));
		List<Map> sourceList = JSONUtil.fromJson(Tools.getDesc(map.get("detail")), new TypeReference<List<Map>>(){});
		List<String> newsList = new ArrayList<>();
		List<String> surveyList = new ArrayList<>();
		for (Map source:sourceList) {
			String type = Tools.getDesc(source.get("link_type")).replace(".0", "");
			String linkId = Tools.getDesc(source.get("link_id")).replace(".0", "");
			if (type.equals(LINK_SURVEY_TYPE)) {
				questIds = questIds + "," + source.get("link_id");
				surveyList.add(LINK_SURVEY_SUFFIX + linkId);
			} else {
				newsIds = newsIds + "," + linkId;
				newsList.add(LINK_NEWS_SUFFIX + linkId);
			}
		}
		map.put("surveys", surveyList);
		map.put("news", newsList);
		map.remove("detail");
	}
	List<Map> visitTitles = QBuilder.db("basedb").select("new map(s.id as id, s.title as title)").from("Survey as s").where(new Where("s.id in(" + parentSurveyIds + ")")).query().getResultObj();
	Map<Integer, String> visitTitleMap = new HashMap();
	for (Map map:visitTitles) {
		visitTitleMap.put((Integer)map.get("id"), Tools.getDesc(map.get("title")));
	}
	Map<Integer, User> userMap = usersByUids(uids);
	Map<String, Map> newsLinkSource = new HashMap<>();
	Map<String, Map> surveyLinkSource = new HashMap<>();
	List<Map> quests = QBuilder.db("basedb").select("new map(id as id, title as title)").from("Survey s").where(new Where("s.status=", STATUS_ENABLE).and("s.id in(" + questIds + ")")).query().getResultObj();
	for (Map map:quests) {
		surveyLinkSource.put(LINK_SURVEY_SUFFIX + map.get("id"),  map);
	}
	List<Map> news = QBuilder.db("basedb").select("new map(id as id, title as title)").from("News n").where(new Where("n.status=", STATUS_ENABLE).and("n.id in(" + newsIds + ")")).query().getResultObj();
	for (Map map:news) {
		newsLinkSource.put(LINK_NEWS_SUFFIX + map.get("id"),  map);
	}
	List<Information> informations = new ArrayList<>();
	for (Map map:visitList) {
		User patient = userMap.get((Integer)map.get("patient_uid"));
		List<String> newsList = (List<String>)map.get("news");
		String newsDesc = "图文须知《-1》";
		for (String str:newsList) {
			Map newsMap = newsLinkSource.get(str)==null?new HashMap(): newsLinkSource.get(str);
			newsDesc = newsDesc + "、《" + Tools.getDesc(newsMap.get("title")) + "》";
		}
		newsDesc = newsDesc + "需要阅读，";
		if (isEmpty(newsList)) 
			newsDesc = "";
		List<String> surveyList = (List<String>)map.get("surveys");
		String surveyDesc = "量表《-1》";
		for (String str:surveyList) {
			Map surveyMap = surveyLinkSource.get(str)==null?new HashMap(): surveyLinkSource.get(str);
			surveyDesc = surveyDesc + "、《" + Tools.getDesc(surveyMap.get("title")) + "》";
		}
		surveyDesc = surveyDesc + "需要填写。";
		if (isEmpty(surveyList)) {
			surveyDesc = "";
			newsDesc = newsDesc.replace("需要阅读,", "需要阅读");
		}
		surveyDesc = surveyDesc.replace("《-1》、", "");	
		newsDesc = newsDesc.replace("《-1》、", "");
		String visitDetail = String.format("今天您的随访《%s》的『%s』节点到了", new Object[]{visitTitleMap.get((Integer)map.get("parent_survey_id")), map.get("title")});
		String saveMessage = String.format("%s，您好，%s，附带了%s%s", new Object[]{patient.getName(), visitDetail, newsDesc,surveyDesc});
		String saveTitle = "随访提醒";
		Information info = new Information();
		info.setUid(patient.getId());
		info.setOid(map.get("parent_survey_id") + "");
		info.setTitle(map.get("title") + "");
		info.setSumary(saveTitle);
		info.setContent(saveMessage);
		info.setCategory(2);
		info.setHospital(hospital);
		informations.add(info);
		String pushMessage = visitDetail + ",请完成相关量表填写和须知阅读";
		String pushTitle = "随访提醒";
		out.println(patient.getId() + ":" + pushMessage + "<br/>");
	}
	if (!isEmpty(informations))
		DBTools.db("basedb").addItem(informations);
%>